(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.docTemplate.createController', createController);
	createController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService','storageService',
			'docTemplateService' ];
	function createController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, storageService, docTemplateService) {
		var ctrl = this, docTemplateInfo = $scope.$stateParams.params, errorMessage = {};
		var log = $scope.loggerFactory.getLogger("docTemplateCreate");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.docTemplate = {};
		ctrl.docTemplate.docTempPreParms = ctrl.docTemplate.docTempPreParms || [];
		ctrl.docTemplate.docTempPostParms = ctrl.docTemplate.docTempPostParms || [];
		
		ctrl.init = function() {
			//init dTempParms
			ctrl.innerPreGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.docTempPreParms.tableConfigs.table, rowSelectionChanged);
			ctrl.innerPreGridOptions.data = 'vm.docTemplate.docTempPreParms';
			ctrl.innerPostGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.docTempPostParms.tableConfigs.table, rowSelectionChanged);
			ctrl.innerPostGridOptions.data = 'vm.docTemplate.docTempPostParms';
			
			function rowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedDTParmList = selectedRows;
			}
			
			authService.create($scope.$stateParams.params).then(function(response) {
				ctrl.docTemplate = response.data || $scope.$stateParams.params;
				ctrl.docTemplate.docTempPreParms = ctrl.docTemplate.docTempPreParms || [];
				ctrl.docTemplate.docTempPostParms = ctrl.docTemplate.docTempPostParms || [];
			});
			// init dataOptions
			docTemplateService.initDataOptions($scope, ctrl);
			
		};
		
		ctrl.createSave = function() {
			docTemplateService.setPrePostParm($scope, ctrl);
			ctrl.docTemplate.docTemplateDocList = ctrl.docTemplate.docTemplateDocList || [];
			if (ctrl.docTemplate.docTemplateDocList && ctrl.docTemplate.docTemplateDocList.length > 0) {				
				ctrl.docTemplate.docId = ctrl.docTemplate.docTemplateDocList[0].docId;
			}
			if (ctrl.docTemplate.taskId) {
				return authService.createContinueSave(ctrl.docTemplate).then(saveSuccessful, saveFailed);
			} else {
				for (var name in ctrl.docTemplate) {
					log.debug(name + '=' + ctrl.docTemplate[name]);
				}
				return authService.createSave(ctrl.docTemplate).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.createSubmit = function() {
			docTemplateService.setPrePostParm($scope, ctrl);
			ctrl.docTemplate.docTemplateDocList = ctrl.docTemplate.docTemplateDocList || [];
			if (ctrl.docTemplate.docTemplateDocList && ctrl.docTemplate.docTemplateDocList.length > 0) {				
				ctrl.docTemplate.docId = ctrl.docTemplate.docTemplateDocList[0].docId;
			}
			if (ctrl.docTemplate.taskId) {
				return authService.createContinueSubmit(ctrl.docTemplate).then(submitSuccessful, saveFailed);
			} else {
				return authService.createSubmit(ctrl.docTemplate).then(submitSuccessful, submitFailed);
			}
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.docTemplate.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.doctemplate.list',
					'state' : 'cda.docTemplate.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.docTemplate = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
